from tkinter import *
from tkinter.simpledialog import *
import tkinter.font as tkFont
from tool_eventer import Eventer
from tool_draw import *

# 幻灯片页

nameset = set() # 名称集合
import random
def randname():
    return "__"+"".join(chr(random.randint(ord('0'), ord('9'))) for i in range(4))


class NetShow3(Frame):
    LampShape = LampShape
    ConnectShape = ConnectShape
    
    
    def __init__(self, master=None):
        Frame.__init__(self, master)
        

        
        # self.canvas = Canvas(self, width=1200, height=800, background=[bglist.pop(), print("hi")][0])                  # 创建控件      background="black"
        self.canvas = Canvas(self, width=2000, height=1000, background="black")                  # 创建控件      background="black"
        # self.canvas.pack(fill=BOTH, expand=1)
        self.canvas.place(anchor=CENTER, relx=0.5, rely=0.5)
        
        
        #self.config(cursor="hand2") # 手形
        
        #G.img = PhotoImage(file="right.png", name="name1")
        #cursorid = self.canvas.create_image(100, 100, image=G.img)
        # 在鼠标旁边画点东西就算了（）……

        NetShow.LampShape.ft = tkFont.Font(family='Fixdsys', size=20, weight=tkFont.BOLD) # tk初始化后才能工作
        

        # self.netcontrol = None      # 携带的信息
        self.lampshape_dict = {}
        self.choosed = None     # 使用于：移动节点， 终止移动，设置连接起点， 绘制临时连接
        self.tempconnectshape = None    # 使用于：绘制临时连接， 删除临时连接
        self.choosed2 = None    # 使用于：绘制临时连接， 删除临时连接， 建立连接
        
        self.config_event()

        


    def config_event(self):
        self.eventer = Eventer(b_canvas=self.canvas.bind, b_tag=self.canvas.tag_bind)
        eventer = self.eventer



    def findlampshape(self, x, y):   # 工具函数
        l = self.canvas.find_overlapping(x-2, y-2, x+2, y+2)
        for id in reversed(l):
            lampshape = self.lampshape_dict.get(id, None)
            if lampshape is not None:
                return lampshape


    def input(self, info):
        lamps, connects = info 
        d = {}
        for name, x, y in lamps:
            if name.startswith("__"):
                lampshape = NetShow.LampShape(self.canvas, x, y, "")
            elif "__" in name:
                lampshape = NetShow.LampShape(self.canvas, x, y, name.split("__")[0])
            else:
                lampshape = NetShow.LampShape(self.canvas, x, y, name)
            self.lampshape_dict[lampshape.rectid] = lampshape
            d[name] = lampshape
        for source, targets in connects.items():
            lamp1 = d[source]
            for target in targets:
                lamp2 = d[target]
                connectshape = NetShow.ConnectShape(self.canvas, lamp1, lamp2, color="green")
                lamp1.connectto.append(connectshape)
                lamp2.connectfrom.append(connectshape)


class Page3:
    def __init__(self, mainwindow):
        self.mainwindow = mainwindow
        self.netshow = NetShow()

    def start(self):
        self.netshow.pack(fill=BOTH, expand=1)

    def end(self):
        self.netshow.pack_forget() # 鼠标事件随之消失  # 键盘事件原则上应该解绑
            

 